package com.tennis.tennisreservation.service.impl;

import com.tennis.tennisreservation.entity.Court;
import com.tennis.tennisreservation.repository.CourtRepository;
import com.tennis.tennisreservation.service.CourtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 场地服务实现类，实现具体的业务逻辑
 */
@Service
public class CourtServiceImpl implements CourtService {

    @Autowired
    private CourtRepository courtRepository;

    @Override
    public Court addCourt(Court court) {
        court.setCreateTime(LocalDateTime.now());
        court.setUpdateTime(LocalDateTime.now());
        return courtRepository.save(court);
    }
    
    @Override
    public Court saveCourt(Court court) {
        LocalDateTime now = LocalDateTime.now();
        if (court.getId() == null) {
            // 新增
            court.setCreateTime(now);
        } else {
            // 修改，保留原创建时间
            Court existingCourt = courtRepository.findById(court.getId()).orElse(null);
            if (existingCourt != null) {
                court.setCreateTime(existingCourt.getCreateTime());
            } else {
                court.setCreateTime(now);
            }
        }
        court.setUpdateTime(now);
        return courtRepository.save(court);
    }

    @Override
    public List<Court> getAllCourts() {
        return courtRepository.findAll();
    }
    
    @Override
    public List<Court> getCourts(String name, Integer status) {
        List<Court> courts = courtRepository.findAll();
        
        // 根据条件过滤
        return courts.stream()
                .filter(court -> {
                    boolean match = true;
                    
                    // 按名称过滤
                    if (StringUtils.hasText(name)) {
                        match = match && court.getName().contains(name);
                    }
                    
                    // 按状态过滤
                    if (status != null) {
                        match = match && court.getStatus().equals(status);
                    }
                    
                    return match;
                })
                .collect(Collectors.toList());
    }
    
    @Override
    public List<Court> getCourts(String name, Integer status, Integer pageNum, Integer pageSize) {
        // 先获取所有符合条件的记录
        List<Court> filteredCourts = getCourts(name, status);
        
        // 计算分页位置
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, filteredCourts.size());
        
        // 确保索引有效
        if (fromIndex >= filteredCourts.size()) {
            return List.of(); // 返回空列表
        }
        
        // 返回分页数据
        return filteredCourts.subList(fromIndex, toIndex);
    }
    
    @Override
    public long countCourts(String name, Integer status) {
        // 简单实现，计算符合条件的记录数
        return getCourts(name, status).size();
    }

    /**
     * 根据ID删除场地
     */
    @Override
    public void deleteCourt(Long id) {
        courtRepository.deleteById(id);
    }
    
    @Override
    public Court updateStatus(Long id, Integer status) {
        Court court = courtRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("场地不存在"));
        
        court.setStatus(status);
        court.setUpdateTime(LocalDateTime.now());
        
        return courtRepository.save(court);
    }
}
